package com.bocai.open.service;

import com.alibaba.fastjson.TypeReference;
import com.bocai.CacheKey;
import com.bocai.config.RedisAdapter;
import com.bocai.open.mapper.OpenMapper;
import com.bocai.open.model.WinRanker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author create by VIC.Huang
 * @date 2018/3/9
 */
@Service
public class OpenService {
    @Autowired
    private OpenMapper openMapper;
    @Autowired
    private RedisAdapter redisAdapter;

    private static final int DAILY = 1;
    private static final int WEEK = 2;
    private static final int MONTH = 3;

    public List<WinRanker> getDailyWinRank() {
        if(redisAdapter.exists(CacheKey.DAILY_WINRANKER)) {
            return redisAdapter.get(CacheKey.DAILY_WINRANKER, new TypeReference<List<WinRanker>>(){});
        }
        List<WinRanker> result = openMapper.getWinRanker(DAILY);
        redisAdapter.setex(CacheKey.DAILY_WINRANKER, 600, result);
        return result;
    }


    public List<WinRanker> getWeekWinRank() {
        if(redisAdapter.exists(CacheKey.WEEK_WINRANKER)) {
            return redisAdapter.get(CacheKey.WEEK_WINRANKER, new TypeReference<List<WinRanker>>(){});
        }
        List<WinRanker> result = openMapper.getWinRanker(WEEK);
        redisAdapter.setex(CacheKey.WEEK_WINRANKER, 3600, result);
        return result;
    }

    public List<WinRanker> getMonthWinRank() {
        if(redisAdapter.exists(CacheKey.MONTH_WINRANKER)) {
            return redisAdapter.get(CacheKey.MONTH_WINRANKER, new TypeReference<List<WinRanker>>(){});
        }
        List<WinRanker> result = openMapper.getWinRanker(MONTH);
        redisAdapter.setex(CacheKey.MONTH_WINRANKER, 36000, result);
        return result;
    }
}
